package com.cqut.hfh.ordering.controller;

import com.cqut.hfh.ordering.pojo.FodFood;
import com.cqut.hfh.ordering.pojo.FodFoodVo;
import com.cqut.hfh.ordering.pojo.FoodMerchant;
import com.cqut.hfh.ordering.pojo.FoodType;
import com.cqut.hfh.ordering.service.IFodPicService;
import com.cqut.hfh.ordering.service.IFoodService;
import com.cqut.hfh.ordering.service.IFoodTypeService;
import com.cqut.hfh.ordering.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * create by  admin on 2019/6/25
 */
@Controller
@RequestMapping("/food")
public class FoodController {
    @Autowired
    private IFoodService foodService;
    @Autowired
    private IFoodTypeService foodTypeService;
    @Resource
    private IFodPicService fodPicService;

    /**
     * 显示所有食品列表
     * @param limit
     * @param page
     * @return
     */
    @RequestMapping(path = "/findAlls",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String getAllFood(@RequestParam("limit") String limit, @RequestParam("page") String page){
        List<Map<String, Object>> food_list = foodService.getAllFood(limit, page);
        init(food_list);
        Integer count = foodService.getCount();
        return Message.getReturnMsg(0,"", count, food_list);
    }

    @RequestMapping(path="/findAll",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String getAllFoodType(@RequestParam("limit") String limit,@RequestParam("page") String page){
        List<Map<String, Object>> foodType_list = foodTypeService.getAllFoodType(limit, page);
        init(foodType_list);
        Integer count = foodTypeService.getCount();
        return Message.getReturnMsg(0,"", count, foodType_list);
    }

    @RequestMapping(path = "/findFoodByName",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findTypeByName(@RequestParam("limit") String limit,@RequestParam("page") String page,
                                 @RequestParam("food_name") String food_name){
        List<Map<String, Object>> food_list = foodService.getFoodByName(limit, page, food_name);
        init(food_list);
        Integer count = foodService.getCount();
        return Message.getReturnMsg(0,"", count, food_list);

    }
    @RequestMapping(path = "/foodAdd",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String foodAdd(FodFood food, FoodMerchant foodMerchant, String pic_site){
        Integer result=foodService.findMerchant(foodMerchant.getMerchant_id(),foodMerchant.getMerchant_name());

        if (result==1){
            foodService.AddFood(food);
            fodPicService.savePic(food.getFood_id(), pic_site);
            return Message.getReturnMsg(0,"");
        }
        return null;
    }

    @RequestMapping(path = "/foodDelete",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String deleteById(@RequestParam("id") String food_id){
        foodService.delById(food_id);
        return Message.getReturnMsg(0,"ok");

    }

    @RequestMapping(path = "/findById")
    @ResponseBody
    public FodFoodVo findById(@RequestParam("id") String food_id){
        Map<String, Object> map = foodService.findById(food_id);
        FodFoodVo food = new FodFoodVo();
        food.setProperties(map);
        return food;
    }

    @RequestMapping(path = "/foodUpdate",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String FoodUpdate(FodFood food,FoodMerchant foodMerchant, String pic_site){
        foodService.updateFood(food,foodMerchant);

        fodPicService.updatePic(food.getFood_id(), pic_site);

        return Message.getReturnMsg(0,"");
    }

    public void init(List<Map<String, Object>> list){
        for( Map<String, Object> map : list){
            if( map.get("isva").toString().equals("1")){
                map.put("isva", "有效");
            }
            if( map.get("isva").toString().equals("0")){
                map.put("isva", "无效");
            }
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tsStr = sdf.format(map.get("optime"));
            map.put("optime", tsStr);
        }
    }


}
